package org.popupcommunication.utils;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;


public abstract class BrowserFiles extends SimpleFileVisitor<Path>{

	public static List<String> getDirectoryFiles(final Path rep, final Path racine) throws IOException {
		final List<String> lstFiles = new ArrayList<String>();
		

		
		System.out.println("browser file: "+rep);
	  if (!Files.isDirectory(rep)) {
	    throw new IllegalArgumentException(
	        "Le chemin n'est pas celui d'un repertoire");
	  }
	  Files.walkFileTree(rep, new SimpleFileVisitor<Path>() {
		  
		 public void addFile(Path fichier){
			 lstFiles.add(racine.relativize(fichier).toString().replace('\\', '/'));
		 } 
	  
	    @Override
	    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
	        throws IOException {
	      if (Files.isRegularFile(file)) {
	    	  addFile(file);
	      }
	      return FileVisitResult.CONTINUE;
	    }
	    
	    @Override
	    public FileVisitResult preVisitDirectory(Path dir,
	        BasicFileAttributes attrs) throws IOException {
	      return FileVisitResult.CONTINUE;
	    }
	  });
	  return lstFiles;
	}

}
